//
//  TJEmotionToolBar.swift
//  testDate
//
//  Created by 唐后能 on 16/4/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

// 定义代理协议.
protocol TJEmotionToolBarDelegate:NSObjectProtocol {

    func emotionToolBar(emotionToolBar:TJEmotionToolBar,emotionType:TJEmotionToolBarBtnType)
}

enum TJEmotionToolBarBtnType:Int {

    case Resent = 0
    case Default = 1
    case Emoji = 2
    case Lxh = 3
}

class TJEmotionToolBar: UIView {

    // 定义代理属性.
    weak var delegate:TJEmotionToolBarDelegate?
    
    // 记录上次选中的按钮.
    private var lastSelectedBtn:UIButton?
    
   // 返回toolBar的方法.
    class func loadEmotionToolBar()->TJEmotionToolBar{
    
        return NSBundle.mainBundle().loadNibNamed("TJEmotionToolBar", owner: nil, options: nil).last as! TJEmotionToolBar

    }
    
    // 按钮点击方法.
    @IBAction func toolBtnClicked(btn: UIButton) {
        
        let btnType = TJEmotionToolBarBtnType(rawValue: btn.tag)
//        print("\(btnType)")
        
        // 切换选中按钮.
        switchBtnStatus(btn)
        
        // 调用代理方法.
        delegate?.emotionToolBar(self, emotionType: btnType!)
    }
    
    // 切换按钮的选中状态.
    private func switchBtnStatus(currentBtn:UIButton) {
    
        // 0.如果多次点击同一个按钮,直接返回.
        if lastSelectedBtn == currentBtn {
        
            return
        }
        
        // 1,将之前的按钮取消选中.
        lastSelectedBtn?.selected = false
        // 2,让当前的按钮选中.
        currentBtn.selected = true
        // 3,把当前选中按钮赋值给之前选中按钮,进行记录.
        lastSelectedBtn = currentBtn
    }
    
    // 切换到与collectonView对应的Btn.
    func switchToBtnWithNum(num:Int) {
    
        let btn = self.subviews[num] as! UIButton
        switchBtnStatus(btn)
    }
    

}
